package query_understanding.query_understand.tree_generator;

import edu.stanford.nlp.ling.IndexedWord;
import edu.stanford.nlp.util.CoreMap;
import org.springframework.stereotype.Component;
import query_understanding.query_understand.tree_progress.DocumentQueryProgress;
import query_understanding.query_language.doc_entity_tree.EntityQueryTree;
import query_understanding.query_language.doc_entity_tree.QueryNode;
import query_understanding.query_language.doc_entity_tree.QueryNodeTokenMap;
import query_understanding.query_understand.tree_progress.EntityQueryProgress;
import semantic_compute.entity_linking.schema.EntityLinkingResult;

import java.util.*;

/**
 * Created by julianzliu on 4/22/2017.
 */
//@Component
public class TopicEntitySelector {


    private DocumentQueryProgress progress;
    private EntityQueryProgress entityProgress;

    public TopicEntitySelector(DocumentQueryProgress progress,EntityQueryProgress entityProgress){
        this.progress = progress;
        this.entityProgress = entityProgress;
    }




    public DocumentQueryProgress selectTopicEntity( ){
         selectTopicEntity( this.entityProgress.entityTokenMap );
         return this.progress;
    }

    private void selectTopicEntity(Map<EntityLinkingResult.LinkedEntity, QueryNodeTokenMap> entityTokenMap){

        CoreMap setence = this.entityProgress.sentence;
        //Map<EntityLinkingResult.LinkedEntity, QueryNodeTokenMap> entityTokenMap = this.entityProgress.entityTokenMap;


        PriorityQueue<EntityQueryTree> allPossibleTrees = this.entityProgress.genEmptyPriorityQueue();

        /*
        这里不能遍历多个句子的mention，
        只能遍历单个句子的。
         */
        for(EntityLinkingResult.LinkedEntity mention : entityTokenMap.keySet() ){
            QueryNodeTokenMap nodeTokenMap = entityTokenMap.get(mention);
            EntityQueryTree topicTree = genSingleTopicEntityTree(mention, nodeTokenMap);
            // 要设置 treeScore
            allPossibleTrees.add(topicTree);
        }

        this.entityProgress.treePriorityQueue = allPossibleTrees;
        this.entityProgress.extendFinishedQueue();
    }



    private EntityQueryTree genSingleTopicEntityTree(EntityLinkingResult.LinkedEntity mention,  QueryNodeTokenMap nodeTokenMap){
        System.out.println("[genSingleTopicEntityTree] mention = " + mention.entity + "  , token = " + nodeTokenMap.mainToken);
        EntityQueryTree entityQueryTree = new EntityQueryTree( this.entityProgress.initTree );


        QueryNode.EntityNode topicNode = new QueryNode.EntityNode();


        topicNode.entity = mention;
        topicNode.mainToken = nodeTokenMap.mainToken;
        topicNode.allTokens = nodeTokenMap.tokenSet;
        topicNode.yago_id = this.progress.entityLinkingResult.entityMetadata.get( mention.entity ).YAGO_ID;
        topicNode.yago_label = topicNode.yago_id;
        topicNode.score = mention.score;
        nodeTokenMap.queryElement = topicNode;

        //entityQueryTree.visited = new HashSet<>();
        //entityQueryTree.curret = nodeTokenMap;
        //entityQueryTree.addIndexWord(topicNode.mainToken, topicNode);
        //nodeTokenMap.queryElement = topicNode;
        entityQueryTree.addQueryNodeTokenMapOfNode( nodeTokenMap );
        entityQueryTree.topicEntity = topicNode;
        entityQueryTree.answerEntity = topicNode;
        entityQueryTree.treeScore = topicNode.score;







        return entityQueryTree;
    }


    /****************************************************
     *
     * @param args
     */
    public static void main(String[] args){

    }



}
